﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Odbc;

namespace caculate
{
    public partial class Problems : Form
    {
        public Problems()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Problems_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;

            string conString = "DSN=test;UID=root;PWD=123456"; //ODBC的数据源名
            OdbcConnection con1 = new OdbcConnection(conString);
            con1.Open();

            string sql1 = "select distinct date from problem ";
            OdbcCommand com1 = new OdbcCommand(sql1, con1);
            OdbcDataReader sdr1 = com1.ExecuteReader();
            while(sdr1.Read())
            {

                comboBox1.Items.Add(sdr1[0]);
                
            }
            
        }


        private void search_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();
            //richTextBox1.Clear();//清空
            string conString = "DSN=test;UID=root;PWD=123456"; //ODBC的数据源名
            OdbcConnection con2 = new OdbcConnection(conString);
            OdbcConnection con3 = new OdbcConnection(conString);
            con2.Open();
            con3.Open();

            string question = "select question,answer from problem where date='"+comboBox1.Text+"'and account='"+Form1.PublicValue.chdaccount+"' ";
            OdbcCommand com2 = new OdbcCommand(question, con2);
            OdbcDataReader sdr2 = com2.ExecuteReader();
            

            while (sdr2.Read())
            {
                string s1 = Convert.ToString(sdr2[0]);
                string s2 = Convert.ToString(sdr2[1]);

                int index = this.dataGridView1.Rows.Add();
                this.dataGridView1.Rows[index].Cells[0].Value = s1+s2;
                index++;
            }
        }

        private void Delete_Click(object sender, EventArgs e)
        {

            int count=0;
            string conString = "DSN=test;UID=root;PWD=123456"; //ODBC的数据源名
            OdbcConnection con5 = new OdbcConnection(conString);
            con5.Open();
            string sql1 = "select distinct power from ChildrenMessage where ChildrenAccount='"+Form1.PublicValue.chdaccount+"' ";
            OdbcCommand com1 = new OdbcCommand(sql1, con5);
            int power = Convert.ToInt32(com1.ExecuteScalar());//是否有权利修改
            if (power == 1)
            {
                OdbcConnection con4 = new OdbcConnection(conString);
                con4.Open();
                string selectRows = "";
                for (int i = 0; i < dataGridView1.Rows.Count; i++) //循环datagridview每行
                {
                    string st = (String)dataGridView1.Rows[i].Cells[0].Value;
                    string[] sArray = st.Split('=');
                    if ((bool)dataGridView1.Rows[i].Cells[1].EditedFormattedValue == true)
                    {
                        string sql = "delete from problem where question='" + sArray[0] + "'and answer='" + "=" + sArray[1] + "' ";
                        OdbcCommand com = new OdbcCommand(sql, con4);
                        int sdr = Convert.ToInt32(com.ExecuteNonQuery());
                        count = count + sdr;
                        selectRows = selectRows + "[" + i.ToString() + "]";

                    }
                }

                MessageBox.Show("Selected Rows:" + selectRows + "删除记录数:" + count, "提示");
            }
            else
            {
                MessageBox.Show("暂无权利修改错题库", "提示");
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.CheckState == CheckState.Checked)//如果全选按钮选中
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if ((bool)dataGridView1.Rows[i].Cells[1].EditedFormattedValue == false)
                    {
                        dataGridView1.Rows[i].Cells[1].Value = "True"; //循环勾选
                    }
                    else
                        continue;
                }
                checkBox1.Text = "反选";
            }
            else
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if ((bool)dataGridView1.Rows[i].Cells[1].EditedFormattedValue == true)
                    {
                        dataGridView1.Rows[i].Cells[1].Value = "False";
                    }
                    else
                        continue;
                }
                checkBox1.Text = "全选";
            }
        }
    }
}
